﻿using System.Linq;
using System.Windows.Forms;

namespace e_TicaretAdmin_Application
{
    public partial class YeniUrun : Form
    {
        public YeniUrun()
        {
            InitializeComponent();
        }

        public int? Id { get; set; }
        private Product _product = new Product();

        private void btnKaydet_Click(object sender, System.EventArgs e)
        {
            _product.Title = txtBaslik.Text;
            _product.Price = double.Parse(txtFiyat.Text);
            _product.Kdv = double.Parse(txtKdv.Text);
            _product.Model = txtMarka.Text;
            _product.Attributes = txtOzellik.Text;
            _product.ImageUrl = txtImageUrl.Text;
            _product.SeoText = txtSeo.Text;
            _product.Description = txtAciklama.Text;
            _product.Category = (Category)comboBox1.SelectedItem;

            if (!Id.HasValue)
                Program.Db.Products.InsertOnSubmit(_product);
            Program.Db.SubmitChanges();
            Close();
        }

        private void YeniUrun_Load(object sender, System.EventArgs e)
        {
            comboBox1.Items.AddRange(Program.Db.Categories.ToArray());
            comboBox1.DisplayMember = "Text";
            comboBox1.ValueMember = "Id";
            if (Id.HasValue)
            {
                _product = Program.Db.Products.SingleOrDefault(d => d.Id == Id);

                comboBox1.SelectedItem = _product.Category;
                txtBaslik.Text = _product.Title;
                txtFiyat.Text = _product.Price.ToString();
                txtKdv.Text = _product.Kdv.ToString();
                txtMarka.Text = _product.Model;
                txtOzellik.Text = _product.Attributes;
                txtImageUrl.Text = _product.ImageUrl;
                txtSeo.Text = _product.SeoText;
                txtAciklama.Text = _product.Description;
            }
        }
    }
}
